import { createRouter, createWebHistory } from 'vue-router';
import HomeView from '../views/Home/HomeView.vue';

const router = createRouter({
  history: createWebHistory(import.meta.env.BASE_URL),
  routes: [
    {
      path: '/',
      name: 'home',
      component: HomeView,
    },
    {
      path: '/login',
      name: 'login',
      component: () => import('../views/Login/LoginView.vue'),
      children: [
        {
          path: 'register',
          name: 'register',
          component: () => import('../views/Login/views/RegisterView.vue'),
          meta: {
            showfater: false,
          },
        },
      ],
      meta: {
        showfater: true,
      },
    },
  ],
});

router.beforeEach(async (to, from) => {
  var auth = localStorage.getItem('token');
  if (to.name == 'login' || to.name == 'register') {
    if (auth) {
      router.push('/');
    }
  } else {
    if (auth) {
      return;
    } else {
      router.push('/login');
    }
  }
});

export default router;
